<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800">
  <div id="app" class="my-app">
    <div class="my-wrapper">
      <el-row :gutter="20">
        <el-col v-for="dict in getDictOptions(DICT_TYPE.ETL_DATASOURCE_TYPE)" :span="8" :key="dict.label" >
          <div  class="my-block"
                :class="[
                  dataSourceStates[dict.label].hovered && 'hovered',
                  dataSourceStates[dict.label].selected && 'selected'
                ]"
                @mouseenter="handleMouseEnter(dict.label)"
                @mouseleave="handleMouseLeave(dict.label)"
                @click="handleClick(dict)">
            <img :src="EtlDatasourceEnum[dict.label]?.img"  alt="Dynamic Image" />
            <div>{{dict.label}}</div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
    <template #footer>
      <el-button  type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import { ref,defineEmits } from 'vue'
import { EtlDatasourceEnum } from '@/utils/constants'

import { DICT_TYPE, getDictOptions } from '@/utils/dict'


defineOptions({ name: 'DatasourceSelect' })


const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('请选择数据源') // 弹窗的标题
/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 创建一个响应式的对象，用于存储每个数据源的状态
const dataSourceStates = reactive({});
for (const dict of getDictOptions(DICT_TYPE.ETL_DATASOURCE_TYPE)) {
  dataSourceStates[dict.label] = {
    hovered: false,
    selected: false
  };
}

const selectedItem = reactive({
  name: null,
  isSelected: false
});

const emit = defineEmits(['datasource-selected']); // 自定义事件

const handleClick = (item) => {
  Object.keys(dataSourceStates).forEach(k => {
    dataSourceStates[k].selected = false;
  });
  dataSourceStates[item.label].selected = true;
  selectedItem.name = item.label;
  selectedItem.isSelected = true;
};



const handleMouseEnter = (index) => {
  dataSourceStates[index].hovered = true;
};

const handleMouseLeave = (index) => {
  dataSourceStates[index].hovered = false;
};

const submitForm = () => {
  if (selectedItem.isSelected) {
    emit('datasource-selected', 'create', selectedItem.name);
    dialogVisible.value = false;
  } else {
    message.warning('请选择一个数据源');
  }
};

</script>
<style scoped>
.my-app {
  display: flex;
  align-items: center;
  justify-content: center;
}
.my-wrapper {
  width: 600px;
  height: 600px;
  border: 1px solid #ccc;
  padding: 20px;
}
.my-block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid #444;
  padding: 10px;
  margin: 10px 0;
  border-radius: 5px;
}
.my-block {
  cursor: pointer;
  transition: all 0.3s ease;
}
.my-block.hovered {
  background-color: rgba(0, 0, 0, 0.1);
}
.my-block.selected {
  border: 2px solid blue;
}
.my-block.hovered div,
.my-block.selected div {
  color: #3a8ee6;
  padding: 5px;
}
</style>
